import {createRouter, createWebHashHistory} from 'vue-router'
import Layout from '@/layout/index.vue'
import {ElMessage} from "element-plus";

const routes = [
    {
        path: "/login",
        name: "Login",
        component: () => import("@/views/login/index.vue"),
    },
    {
        path: "/register",
        name: "Register",
        component: () => import("@/views/register/index.vue"),
    },
    {
        path: "/",
        name: "Home",
        component: Layout,
        redirect: '/home',
        children: [
            {
                path: "/home",
                name: "home",
                component: () => import("@/views/home/index.vue"),
            }
        ]
    },
    {
        path: "/system",
        name: "system",
        component: Layout,
        redirect: '/system',
        children: [
            {
                path: "system",
                name: "系统管理",
                children: [
                    {
                        path: "/system/user",
                        name: "用户管理",
                        component: () => import("@/views/user/user.vue"),
                    }
                ],
            },
        ]
    }
];
const router = createRouter({
    history: createWebHashHistory(),
    routes

});

router.beforeEach((to, _from, next) => {
    const routes = router.getRoutes();
    if(routes.map(r => r.path).includes(to.path)){
        next();
    }else {
        ElMessage.error("路由表404")
    }
})
export default router;
